package com.share.practice;


public class TestHashMap<K, V> {
	private Entry<K, V>[] table;
	
	private class Entry<K, V> {
		int hash;
		K key;
		V value;
		Entry<K, V> next;
		
		Entry(K key, V value, int hash, Entry<K, V> next) {
			this.key = key;
			this.value = value;
			this.hash = hash;
			this.next = next;
		}
	}
	
	private int getHash(K key) {
		return getHash(key)% (table.length-1);
	}
	
	public void put(K key, V value) {
		int hash = getHash(key);
		Entry<K, V> entry = new Entry<>(key, value, hash, null);
		Entry<K, V> head = table[hash];
		while (head != null) {
			if (head.key.equals(key)) {
				head.value = value;
				return;
			}
			head = head.next;
		}
		
		entry.next = table[hash];
		table[hash] = entry;
	}
	
	public V get(K key){
		int hash = getHash(key);
		Entry<K, V> head = table[hash];
		while (head != null) {
			if (head.key.equals(key)) {
				return head.value;
			}
			head = head.next;
		}
		return null;
	}

}
